home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / LW32.ZIP / TAP11.ZIP / tapsamp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-04  |  7.7 KB  |  301 lines

  1. //
  2. // TAP
  3. // Sample Application
  4. // Revision 1.10
  5. //
  6. //  1/5/95    First created
  7. //
  8. #define INCL_DOS
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "tapapp.h"
  13.  
  14. //
  15. // void RegisterSampleApplication(char *szProgramPath);
  16. //
  17. // Registers the TAP application so that any
  18. // TAP server on the system can find it. This function
  19. // need only be called once.
  20. //
  21. // Parameters:
  22. // szProgramPath
  23. //   Fully qualified path to the executable. Must include .EXE
  24. //   extension.
  25. //   EX: C:\TAPSAMP.EXE
  26. //
  27. // Returns: Nothing
  28. //
  29. void RegisterSampleApplication(char *szProgramPath)
  30. {
  31. TAPAPPENTRY TapAppEntry;
  32.  
  33.     // Fill in information structure
  34.     TapAppEntry . cb = sizeof(TAPAPPENTRY);
  35.  
  36.     // Each TAP application can have a description
  37.     // up to 256 characters and 6 lines long
  38.     strcpy(TapAppEntry . szDescription,
  39.                 "This is a sample TAP application "      //
  40.                 "used for development purposes.\n\r\n\r" //
  41.                 "Each file encountered is displayed "    //
  42.                 "along with its data as the transfer "   //
  43.                 "progresses." );
  44.  
  45.     // Any extra parameters needed to be passed go here
  46.     strcpy(TapAppEntry . szParams, "");
  47.  
  48.     strcpy(TapAppEntry . szProgram,
  49.                 szProgramPath);
  50.  
  51.    // No extensions for this application
  52.    TapAppEntry . ulExtensions = 0;
  53.  
  54.     //
  55.     // Register this TAP application
  56.     //
  57.     // This registration can be removed later with
  58.     // DeRegisterApplication_TAP
  59.     //
  60.     if (!RegisterApplication_TAP("TAP Development Sample",
  61.                                              &TapAppEntry))
  62.         printf("Error : Could not register TAP application\n");
  63.     else
  64.         printf("Application registered successfully\n");
  65.  
  66. }
  67.  
  68. //
  69. // void ProcessData(HFILE TAPFile,
  70. //                          long lCurrentFileSize);
  71. //
  72. // This function is called when new data needs to
  73. // be processed by the TAP application. This is the
  74. // place where a GIF viewer might interpret new data
  75. // and display it as a picture on the screen.
  76. //
  77. // The code here is only a sample of what can be done,
  78. // and if you are basing your TAP application on this
  79. // source it's best if you replace this entire function.
  80. //
  81. // Parameters:
  82. // TAPFile
  83. //   OS/2 file handle (for use with Dosxxx commands)
  84. //   that has read-only access to the file being
  85. //   transferred.
  86. // lCurrentFileSize
  87. //   The current file size. Attempts to read past
  88. //   past this point will normally fail.
  89. //
  90. // Returns: Nothing
  91. //
  92. void ProcessData(HFILE TAPFile,
  93.                         long lCurrentFileSize)
  94. {
  95. unsigned char szBuf[10];
  96. ULONG BytesRead;
  97. APIRET rc = 0;
  98. int cnter;
  99.  
  100.     // Seek to the end of the file,
  101.     // minus ten bytes
  102.     rc |=
  103.         DosSetFilePtr(TAPFile,
  104.             lCurrentFileSize - 10,
  105.             FILE_BEGIN,
  106.             &BytesRead);
  107.  
  108.     // Read those ten last bytes
  109.     rc |=
  110.         DosRead(TAPFile,
  111.           szBuf,
  112.           10,
  113.           &BytesRead);
  114.  
  115.     // If we didn't read ten bytes,
  116.     // there was an error
  117.     if (BytesRead<10)
  118.             rc = 1;
  119.  
  120.     if (rc)
  121.         // Something went wrong
  122.         printf("%8d : error reading\n", lCurrentFileSize);
  123.     else
  124.     {
  125.         // Print current file size
  126.         printf("%8d Data :", lCurrentFileSize);
  127.  
  128.         // And then display last 10 bytes of data
  129.         // in hexidecimal format
  130.         for (cnter=0; cnter<10; cnter++)
  131.             printf(" %2.2X", szBuf[cnter]);
  132.  
  133.         // Carriage return: don't fill up the
  134.         // screen w
  135.         printf("%c",13);
  136.         // Need to flush if our compiler has buffered output
  137.         // (Note that buffered output is only flushed for LF, not CR !)
  138.         fflush (stdout);
  139.     }
  140. }
  141.  
  142. //
  143. // int main(int argc, char **argv);
  144. //
  145. // Main TAP function.
  146. //
  147. // This function registers the TAP
  148. // application, then attempts to start
  149. // a TAP session.
  150. //
  151. // The standard structure of a TAP
  152. // application must be:
  153. //
  154. // Initialize();
  155. // while (NextFile)
  156. //    {
  157. //        OpenFile();
  158. //    while (MoreData)
  159. //            ProcessData();  <-- Here's where your
  160. //    ProcessData();     <-- application does its work
  161. //        CloseFile();
  162. //  }
  163. // DeInitialize();
  164. //
  165. // Parameters:
  166. // It's important to have the argc
  167. // and argv information around because
  168. // TAP initialize must parse the
  169. // command line.
  170. //
  171. // Returns: Return value unimportant to TAP
  172. //
  173. int main(int argc, char **argv)
  174. {
  175. PTAPAPPINFO pTapAppInfo;      // TAP instance data structure
  176.                                         // (You needn't worry about this)
  177.  
  178.     //
  179.     // Attempt to register this application so
  180.     // TAP servers can see it.
  181.     //
  182.     // The full path, as below, must be passed.
  183.     // The program name is kept in argv[0] (ANSI standard)
  184.     // but some compilers handle this differently.
  185.     //
  186.     // Borland C will give you the fully qualified path
  187.     // while CSet will give you only the application name
  188.     // typed at the command prompt.
  189.     //
  190.     // If you're one of those lucky people who are only
  191.     // passed the file name you can construct a fully qualified
  192.     // path name by calling DosQueryPathInfo with 2nd parameter
  193.     // of FIL_QUERYFULLNAME.
  194.     //
  195.  
  196.     // Les: I suggest you *always* call DosQueryPathInfo even though
  197.    //      Borland C already gives you the full name: DosQueryPathInfo
  198.    //      makes sure that the returned path is valid, etc. etc...
  199.  
  200.     {  CHAR szFullPath[CCHMAXPATH];
  201.         strcpy (szFullPath, argv[0]);
  202.         if (!strchr (szFullPath, '.'))
  203.             strcat (szFullPath, ".EXE");
  204.         DosQueryPathInfo (szFullPath, FIL_QUERYFULLNAME, szFullPath, sizeof (szFullPath));
  205.         RegisterSampleApplication(szFullPath);
  206.         }
  207.  
  208.     // Initialize the TAP subsystem
  209.     pTapAppInfo = InitializeApplication_TAP(argc, argv);
  210.  
  211.     // On success we get a non-NULL pointer back
  212.     if (pTapAppInfo)
  213.     {
  214.         CHAR szBuffer[32];
  215.         printf("Application initialized successfully\n\n");
  216.  
  217.         QueryServerVersion_TAP (pTapAppInfo, szBuffer, sizeof (szBuffer));
  218.  
  219.         printf ("Server Version: %s\n", szBuffer);
  220.  
  221.         // NextFile_TAP will wait for the next file to arrive.
  222.         // If no more files are pending, NextFile_TAP will
  223.         // return FALSE, otherwise TRUE
  224.         while (NextFile_TAP(pTapAppInfo))
  225.         {
  226.         char szFileName[CCHMAXPATH];
  227.         long lCompleteFileSize;
  228.         long lCurrentFileSize;
  229.         HFILE TAPFile;
  230.  
  231.             // Get the fully qualified name of the file
  232.             QueryFileName_TAP(pTapAppInfo, szFileName);
  233.  
  234.             // Get the complete size of the file
  235.             // -1 (TAP_SIZE_UNKNOWN) if we don't know it
  236.             lCompleteFileSize = QueryCompleteSize_TAP(pTapAppInfo);
  237.  
  238.             // Display status
  239.             printf("File : %s of size %d\n",
  240.                         szFileName,
  241.                         lCompleteFileSize);
  242.  
  243.             // Open the TAP file. The file must be opened
  244.             // using this TAP API so file sharing can take place
  245.             if (!OpenFile_TAP(pTapAppInfo, &TAPFile))
  246.                 printf("Error opening file!\n");
  247.  
  248.             // MoreData_TAP waits until new data has been
  249.             // written to the file.
  250.             //
  251.             // If the end of file has been reached, or
  252.             // the transfer has been cancelled MoreData_TAP
  253.             // returns FALSE, otherwise TRUE.
  254.             while (MoreData_TAP(pTapAppInfo))
  255.             {
  256.                 // Get the current file size
  257.                 // This will always be different from the last time
  258.                 lCurrentFileSize = QueryCurrentSize_TAP(pTapAppInfo);
  259.  
  260.                 // Now process any new data
  261.                 ProcessData(TAPFile,
  262.                                 lCurrentFileSize);
  263.             }
  264.  
  265.             // Determine complete file length,
  266.             // which may have changed if the
  267.             // file was cut short
  268.             DosSetFilePtr(TAPFile,
  269.                                 0,
  270.                                 FILE_END,
  271.                                 (PULONG)(PVOID)&(lCompleteFileSize));
  272.  
  273.             // There may be some data we didn't
  274.             // finish processing. Process all
  275.             // data before opening a new file
  276.             ProcessData(TAPFile,
  277.                             lCompleteFileSize);
  278.  
  279.             printf("\nFile complete at %d bytes\n\n", lCompleteFileSize);
  280.  
  281.             // Close the file: processing complete
  282.             CloseFile_TAP(pTapAppInfo, TAPFile);
  283.         }
  284.  
  285.         // We've dropped out of the main loop
  286.         // so there are no more files
  287.         printf("Transfer complete.\n");
  288.  
  289.         // Deinitialize the TAP application
  290.         DeInitializeApplication_TAP(pTapAppInfo);
  291.     }
  292.      else
  293.     {
  294.         printf("Error : Cannot initialize TAP application\n\n");
  295.         printf("Start this application from a TAP server\n");
  296.     }
  297.  
  298.     return 0;
  299. }
  300.  
  301.